package com.central.apps.controller;

import com.central.app.service.HandlerService;
import com.central.common.util.ToolUtil;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SubmissionController {
    @Autowired
    private HandlerService handlerService;

    @PostMapping("/generateWO")
    public String generate(@RequestBody Map<String,Object> params) throws Exception {
        JSONObject json = new JSONObject();
        json.put("code", "0");
        String submitnum = MapUtils.getString(params,"submitnum");
        String status = MapUtils.getString(params,"status");
        String type = MapUtils.getString(params,"type");

        // 判断流程状态-3:审批通过
        if (!status.equals("3")) {
            json.put("msg", "流程未审批通过, 请审批通过后再生成工单!");
            return json.toString();
        }

        // 查询提报信息
        List<Map<String, Object>> resultList = handlerService.findBySql("select * from gq_submission where submitnum ='" + submitnum + "'");

        if (CollectionUtils.isNotEmpty(resultList)) {
            // 获取提报信息
            Map<String, Object> resultMap = resultList.get(0);

            String id = MapUtils.getString(resultMap,"id");
            String assetnum = MapUtils.getString(resultMap,"assetnum");
            String assetdesc = MapUtils.getString(resultMap,"assetdesc");
            String location = MapUtils.getString(resultMap,"location");
            String locationdesc = MapUtils.getString(resultMap,"locationdesc");
            String description = MapUtils.getString(resultMap,"description");

            // 组织工单信息
            Map<String, Object> map = new HashMap<>();
            map.put("assetnum", assetnum);
            map.put("assetdesc", assetdesc);
            map.put("location", location);
            map.put("locationdesc", locationdesc);
            map.put("description", description);

            String wotype = "";// 2:缺陷工单; 1:故障工单
            if (type.equals("2")) {
                wotype = "2";
            } else {
                wotype = "1";
            }
            map.put("wotype", wotype);

            // 生成工单编码
            Map<String, Object> woMap = new HashMap<>();
            handlerService.addBizNum("workorderApp", "gq_workorder", woMap);
            // 查询工单编码
            String wonum = (String) woMap.get("wonum");
            map.put("wonum", wonum);
            map.put("status", "1");
            // 插入工单信息
            handlerService.insert( "gq_workorder", map);

            // 更新提报工单信息
            handlerService.updateBySql("update gq_submission set wonum ='"+ wonum + "', wodesc = '"
                    + description + "' where submitnum ='" + submitnum + "'");
        }

        json.put("msg", "生成工单成功, 请到工单管理界面查询数据!");
        return json.toString();
    }
}
